<?php

namespace Maka\Tool\Imail;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

class Mail
{
    public function __construct()
    {
        $mail = new PHPMailer();

        //Server settings
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
        $mail->isSMTP();                                            // Send using SMTP
        $mail->Host       = 'smtp.qq.com';                    // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = '1841864490@qq.com';                     // SMTP username
        $mail->Password   = 'gclcbztlekulcefh';                               // SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

        $mail->setFrom('1841864490@qq.com', '阳羽科技');

        $this->mail = $mail;
    }

    // 发送带附件的邮件
    public function sendAtt($to, $file, $filename = '')
    {
        try {
            //Recipients
            $this->mail->addAddress($to);               // Name is optional

            // 添加附件
            $this->mail->addAttachment($file, $filename);    // Optional name

            // Content
            $this->mail->isHTML(true);                                  // Set email format to HTML
            $this->mail->Subject = "阳羽科技源码商店";
            $this->mail->Body    = "您已成功购买 <b>$filename</b> 请查看附件。";

            return $this->mail->send();
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }


    // 发送邮件
    public function send($to, $body)
    {
        try {
            //Recipients
            $this->mail->addAddress($to);               // Name is optional

            // Content
            $this->mail->isHTML(true);                                  // Set email format to HTML
            $this->mail->Subject = "阳羽科技源码商店";
            $this->mail->Body    = $body;

            return $this->mail->send();
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }
}
